/***************************************************************************
 *   Copyright (C) 2008 by Eduardo Gurgel Pinho                            *
 *   edgurgel@gmail.com                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/

#include "sphere.h"

#include "math/ray.h"
#include "material/object_material.h"

#include <limits>
#include <cmath>

namespace Sirat {

namespace Object {

Sphere::Sphere(Material::Object_Material* _object_material,const Math::Vector& _center = Math::Vector(0.0,0.0,0.0),const double _radius = 0.5) : Object(_object_material), center(_center) , radius(_radius) { }

Sphere::Sphere() : Object(), center(Math::Vector(0.0,0.0,0.0)), radius(1.0) { }

Sphere::Sphere(const Sphere& _sphere) : Object(_sphere) {
	radius = _sphere.radius;
	center = _sphere.center;
	
}

Sphere::~Sphere() { }

Math::Vector Sphere::get_center() const { return center; }

double Sphere::get_radius() const { return radius; }

Math::Vector Sphere::get_normal(const Math::Vector& _point) const { return _point.sub_vect(center).normalize(); }

void Sphere::set_center(const Math::Vector& _center) { center = _center; }

void Sphere::set_radius(const double _radius) { radius = _radius; }

tuple<double,Math::Vector,Object*> Sphere::intersection(const Math::Ray& _ray) {
	std::numeric_limits<double> double_limit;
	const double infinity = double_limit.infinity();
	
	/* Ax² + Bx + C = 0 */
	
	Math::Vector intersection_point;
    
	double delta,distance=infinity;
	const double a = 1.0;
	const double b = 2.0*(_ray.get_direction().dot_product(_ray.get_origin()) - _ray.get_direction().dot_product(center));
	const double c = std::abs(_ray.get_origin().dot_product(_ray.get_origin()) - 2.0*_ray.get_origin().dot_product(center) + center.dot_product(center)) - radius*radius;
	delta = b*b - 4.0*a*c;
	if (delta >= 0.0) {
		double raiz2 = (-b - std::sqrt(delta))/2.0*a;
		if(raiz2 < 0.0) raiz2 = infinity;
		else distance = raiz2;
		if(distance >= 0.0) {
			intersection_point = _ray.get_origin() + _ray.get_direction()*distance;
			return make_tuple(distance,intersection_point,this);
		}
	}
	return make_tuple(infinity,intersection_point,this);
	
}

tuple<double,double> Sphere::get_u_v(const Math::Vector& _point) const {
	
	/**
	 * Cylindrical Coordinates 
	 */
	
	Math::Vector vec = (_point - center)*(1.0/radius);
	
	
	const double v = -vec.y;
	const double u = std::atan2(vec.z,vec.x);

	return make_tuple((u+M_PI)/(2.0*M_PI), (v+1.0)/2.0);
	
}

Sphere& Sphere::operator=(const Sphere& _sphere) {
	Object::operator=(_sphere);
	radius = _sphere.radius;
	center = _sphere.center;
	return *this;
}

}

}
